package com.qiaotongtianxia.loans.fragments;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.activity.RefundNoticeActivity;
import com.qiaotongtianxia.loans.activity.UploadRefundLiceseActivity;
import com.qiaotongtianxia.loans.adapters.WaitRefundAdapter;
import com.qiaotongtianxia.loans.bean.Refund;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.functionInterface.IClickListener;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.Constants;
import com.qiaotongtianxia.loans.views.DragLayout;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.Action;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.RefreshRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/15.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class RefundFragment extends BaseFragment implements DragLayout.DragListener {

    private final int REFUND = 1000;
    @BindView(R.id.refresh_view)
    RefreshRecyclerView refreshView;
    @BindView(R.id.dragLayout)
    DragLayout dragLayout;

    private WaitRefundAdapter adapter;

    private int pos = 0;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_refund;
    }

    @Override
    public Api onApiCreate() {
        return new Api(context);
    }

    @Override
    public void created(View view) {
        dragLayout.setListener(this);
        refreshView.setLayoutManager(new LinearLayoutManager(context));
        refreshView.setLoadMoreAble(false);
        adapter = new WaitRefundAdapter(context);

        refreshView.setAdapter(adapter);

        refreshView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                requestDatas();
            }
        });
        adapter.setiClickListener(new IClickListener<Refund>() {
            @Override
            public void onClick(Refund refund, int positon) {
                pos = positon;
                Intent intent = new Intent(context, UploadRefundLiceseActivity.class);
                intent.putExtra(Constants.REFUND_ID,refund.getId());
                startActivityForResult(intent,REFUND);
            }
        });
        requestDatas();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REFUND && resultCode == Activity.RESULT_OK){
            adapter.remove(pos);
        }
    }

    private void requestDatas() {
        api.waitRefund(new IBaseRequestImp<List<Refund>>() {
            @Override
            public void onRequestSuccess(List<Refund> refunds) {
                adapter.clear();
                adapter.addAll(refunds);
                refreshView.dismissSwipeRefresh();
            }
            @Override
            public void onRequestError(int errorCode, String errorMsg) {
                refreshView.dismissSwipeRefresh();
            }
        });
    }

    @OnClick(R.id.tv_notice)
    public void onViewClicked() {
        Intent intent = new Intent(context, RefundNoticeActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean isCanPullDown() {
        return false;
    }

    @Override
    public boolean isCanPullUp() {
        return CommonUtils.isReachBottom(refreshView.getRecyclerView());
    }
}
